package pl.druminski.robinhood.model;

import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.table.AbstractTableModel;

/**
 * @author Łukasz Drumiński
 * 
 */
public class PluginTableModel extends AbstractTableModel
{

    Data data;
    int columnCount = 5;
    int chckBoxColumn = 1;

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex)
    {
        Boolean editable = false;

        if (columnIndex == chckBoxColumn)
        {
            editable = true;
        }

        return editable;
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex)
    {
        if (chckBoxColumn == columnIndex)
        {
            Boolean value = (Boolean) aValue;
            data.pluginInfo.get(rowIndex).setLoad(value);
            fireTableCellUpdated(rowIndex, columnIndex);
        }
    }

    public PluginTableModel(Data data)
    {
        this.data = data;
    }

    public int getRowCount()
    {
        return data.pluginInfo.size();
    }

    public int getColumnCount()
    {
        return columnCount;
    }

    public Class getColumnClass(int c)
    {
        return getValueAt(0, c).getClass();
    }

    public Object getValueAt(int rowIndex, int columnIndex)
    {
        PluginInfo info = data.pluginInfo.get(rowIndex);
        Object obj = null;

        switch (columnIndex)
        {
            case 0:
                obj = info.getName();
                break;
            case 1:
                obj = (Boolean) info.load();
                break;
            case 2:
                obj = info.getClicks();
                break;
            case 3:
                double d = info.getClicks()*info.getValueOfOneClickInPennies();
                d = d/100;
                obj = d;
                break;
            case 4:

                Calendar now = Calendar.getInstance();
                Calendar age = new GregorianCalendar();

                age.set(info.getAge().getYear(), info.getAge().getMonth()-1, info.getAge().getDay());

                int days = (int) (now.getTime().getTime() - age.getTime().getTime()) / (1000 * 60 * 60 * 24);
                obj = days;
                break;
        }

        return obj;
    }
}
